init-swagger-ui.js ➔ loadSwaggerUI   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 28
c 0
b 0
f 0
dl 0
loc 45
rs 8.7413
1
// This file is part of the API Platform project.
2
//
3
// (c) Kévin Dunglas <[email protected]>
4
//
5
// For the full copyright and license information, please view the LICENSE
6
// file that was distributed with this source code.
7
8
function loadSwaggerUI(userOptions = {}) {
9
    const data = JSON.parse(document.getElementById('swagger-data').innerText);
10
    const defaultOptions = {
11
        spec: data.spec,
12
        dom_id: '#swagger-ui',
13
        validatorUrl: null,
14
        presets: [
15
            SwaggerUIBundle.presets.apis,
0 ignored issues
show
Bug introduced by
The variable SwaggerUIBundle seems to be never declared. If this is a global, consider adding a /** global: SwaggerUIBundle */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
16
            SwaggerUIStandalonePreset
0 ignored issues
show
Bug introduced by
The variable SwaggerUIStandalonePreset seems to be never declared. If this is a global, consider adding a /** global: SwaggerUIStandalonePreset */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
17
        ],
18
        plugins: [
19
            SwaggerUIBundle.plugins.DownloadUrl
20
        ],
21
        layout: 'StandaloneLayout'
22
    };
23
    const options = Object.assign({}, defaultOptions, userOptions);
24
    const ui = SwaggerUIBundle(options);
25
26
    const storageKey = 'nelmio_api_auth';
27
28
    // if we have auth in storage use it
29
    if (sessionStorage.getItem(storageKey)) {
0 ignored issues
show
Bug introduced by
The variable sessionStorage seems to be never declared. If this is a global, consider adding a /** global: sessionStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
30
        try {
31
            ui.authActions.authorize(JSON.parse(sessionStorage.getItem(storageKey)));
32
        } catch (ignored) {
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
33
            // catch any errors here so it does not stop script execution
34
        }
35
    }
36
37
    // hook into authorize to store the auth in local storage when user performs authorization
38
    const currentAuthorize = ui.authActions.authorize;
39
    ui.authActions.authorize = function (payload) {
40
        sessionStorage.setItem(storageKey, JSON.stringify(payload));
0 ignored issues
show
Bug introduced by
The variable sessionStorage seems to be never declared. If this is a global, consider adding a /** global: sessionStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
41
        return currentAuthorize(payload);
42
    };
43
44
    // hook into logout to clear auth from storage if user logs out
45
    const currentLogout = ui.authActions.logout;
46
    ui.authActions.logout = function (payload) {
47
        sessionStorage.removeItem(storageKey);
0 ignored issues
show
Bug introduced by
The variable sessionStorage seems to be never declared. If this is a global, consider adding a /** global: sessionStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
48
        return currentLogout(payload);
49
    };
50
51
    window.ui = ui;
52
}